package com.lazason.wib4android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BasicActivity extends Activity {

	static final String WIB = "com.lazason.wib";
	static final  int mode = Activity.MODE_PRIVATE;
	static int layoutResource;
	
	 TabMenu.MenuBodyAdapter []bodyAdapter=new TabMenu.MenuBodyAdapter[1];  
	    TabMenu.MenuTitleAdapter titleAdapter;  
	    TabMenu tabMenu;  
	    int selTitle=0;  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		//titleAdapter = new TabMenu.MenuTitleAdapter(this, new String[] { }, 16, 0xFF222222,Color.LTGRAY,Color.WHITE);  
		titleAdapter = null;
        //定义每项分页栏的内容  
        bodyAdapter[0]=new TabMenu.MenuBodyAdapter(this,new String[] { "常用1", "常用2", },   
                 new int[] { R.drawable.icon,  
                R.drawable.icon},13, 0xFFFFFFFF);  
           
      
           
        tabMenu=new TabMenu(this,  
                 null,  
                 new BodyClickEvent(),  
                 titleAdapter,  
                 0x55123456,//TabMenu的背景颜色  
                 R.style.PopupAnimation);//出现与消失的动画  
           
         tabMenu.update();  
         //tabMenu.SetTitleSelect(0);  
         tabMenu.SetBodyAdapter(bodyAdapter[0]);  
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		MyApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		MenuItem model =menu.findItem(R.id.model);
		if("night".equals(MyApplication.style )){
			model.setTitle(R.string.normal_model);
		} else {
			model.setTitle(R.string.night_model);
		}

		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.exit:
			MyApplication.getInstance().exit();
			return true;
		case R.id.logout:
			Intent intent = new Intent();
			intent.setClass(this, Welcome.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
			
			
			SharedPreferences mySharedPreferences = getSharedPreferences(WIB, mode);
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			editor.remove("token");
			editor.remove("tokenSecret");
			editor.commit();
			startActivity(intent);
			
			return true;
		case R.id.postweibo:
			Intent postIntent = new Intent();
			postIntent.setClass(this, PostWeiboActivity.class);
			startActivity(postIntent);
			return true;
		
		case R.id.mention:
			Intent mentionIntent = new Intent(); 
			mentionIntent.setClass(this, MentionActivity.class);
			startActivity(mentionIntent);
			return true;
		case R.id.comments:
			Intent commentsIntent = new Intent();
			commentsIntent.setClass(this, CommentsActivity.class); 
			startActivity(commentsIntent);
			return true;
		case R.id.model:
			Intent modelIntent = new Intent();
			modelIntent.setClass(this, this.getClass()); 
			SharedPreferences msharedPreferences = getSharedPreferences(WIB, mode);
			SharedPreferences.Editor sharedPreferencesEditor = msharedPreferences.edit();
			
			
			if("night".equals(MyApplication.style )){
				sharedPreferencesEditor.putString("style", "nomal");
				MyApplication.style = "normal";
			} else {
				sharedPreferencesEditor.putString("style", "night");
				MyApplication.style = "night";
			}
			sharedPreferencesEditor.commit();
			startActivity(modelIntent);
			return true;
		case R.id.message:
			Intent messageIntent = new Intent();
			messageIntent.setClass(this, MessageActivity.class); 
			startActivity(messageIntent);
			return true;
			
		case R.id.pulltorefresh:
			Intent pullToRefreshIntent = new Intent();
			pullToRefreshIntent.setClass(this, PullToRefreshActivity.class); 
			startActivity(pullToRefreshIntent);
			return true;
		case R.id.timeline:
			Intent timelineIntent = new Intent();
			timelineIntent.setClass(this, TimelineActivity.class); 
			startActivity(timelineIntent);
			return true;
			
		}
		return false;
	}
	  class TitleClickEvent implements OnItemClickListener{  
	        @Override  
	        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
	                long arg3) {  
	            selTitle=arg2;  
	            tabMenu.SetTitleSelect(arg2);  
	            tabMenu.SetBodyAdapter(bodyAdapter[arg2]);  
	        }  
	    }  
	      
	    class BodyClickEvent implements OnItemClickListener{  
	        @Override  
	        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
	                long arg3) {  
	            tabMenu.SetBodySelect(arg2,Color.GRAY);  
	            String str="第"+String.valueOf(selTitle)+"栏/n/r"  
	            +"第"+String.valueOf(arg2)+"项";  
	            Toast.makeText(BasicActivity.this, str, 500).show();  
	              
	        }  
	          
	    }  
	   
	    @Override  
	    /** 
	     * 拦截MENU 
	     */  
	    public boolean onMenuOpened(int featureId, Menu menu) {  
	        if (tabMenu != null) {  
	            if (tabMenu.isShowing())  
	                tabMenu.dismiss();  
	            else {  
	                tabMenu.showAtLocation(findViewById(layoutResource),  
	                        Gravity.BOTTOM, 0, 0);  
	            }  
	        }  
	        return false;// 返回为true 则显示系统menu  
	    }  
	    

	
}
